package model;

import com.ansj.vec.Word2VEC;
import entity.News;
import utils.IOUtils;
import utils.SplitWord;
import utils.Transform;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * Created by 梅晨 on 2017/7/20.
 */
public class PreProcess {
    protected HashMap<String,Integer> filePathClaMap;
    protected String modelPath;//word2vec模型地址
    protected String dicPath;
    protected String libSVMPath;
    public PreProcess(){
    }

    public PreProcess(HashMap<String, Integer> filePathClaMap, String modelPath, String dicPath, String libSVMPath) {
        this.filePathClaMap = filePathClaMap;
        this.modelPath = modelPath;
        this.dicPath = dicPath;
        this.libSVMPath = libSVMPath;
    }

    /**
     * 根据传入的文件地址集合，创建news对象，并在news对象内进行分词
     * @return
     * @throws IOException
     */
    protected List<News> newObj()throws IOException{
        List<News> newsList= new ArrayList<News>();
        for(String filePath : filePathClaMap.keySet()){
            String content = IOUtils.getFileContent(filePath);
            HashSet<String> wordTypes = new HashSet<String>();
            wordTypes.add("n");
            wordTypes.add("adj");
            wordTypes.add("v");
            SplitWord splitWord = new SplitWord(wordTypes);
            News news = new News(filePath,content,filePathClaMap.get(filePath),splitWord);
            newsList.add(news);
        }
        return newsList;
    }

    /**
     * 由加载的word2vec模型将newslist中每个新闻的词转换成词向量，并加权成文章的表示，然后存到指定的文件夹中
     * @param newsList
     * @param word2VEC
     */
    protected void VecProcess(List<News> newsList, Word2VEC word2VEC)throws IOException{
        for(News news : newsList){
            List<String> words = news.getWords();
            List<List<Float>> wordsVec = new ArrayList<List<Float>>();
            for(String word : words){
                List<Float> wordVec = new ArrayList<Float>();
                float[] f = word2VEC.getWordVector(word);
                if(f != null){
                    for(float n : f){
                        wordVec.add(n);
                    }
                    wordsVec.add(wordVec);
                }
            }
            List<Float> paraVec = Transform.getParaVec(wordsVec);
            news.setVec(paraVec);
        }
        List<String> libsvmList = Transform.trans2lisvm(newsList);
        IOUtils.saveFile(libsvmList,libSVMPath);
    }
}
